#!/bin/bash

# 角色管理接口404修复验证脚本
# 作者: Wu.Liang
# 日期: 2024-12-21

echo "=== 角色管理接口404修复验证 ==="
echo "开始时间: $(date)"
echo ""

# 1. 获取验证码
echo "1. 获取验证码..."
CAPTCHA_RESPONSE=$(curl -s -X GET http://127.0.0.1:8000/api/user/auth/captcha)
echo "验证码响应: $CAPTCHA_RESPONSE"

# 提取验证码信息
CAPTCHA_KEY=$(echo $CAPTCHA_RESPONSE | grep -o '"captchaKey":"[^"]*"' | cut -d'"' -f4)
CAPTCHA_CODE=$(echo $CAPTCHA_RESPONSE | grep -o '"captchaCode":"[^"]*"' | cut -d'"' -f4)

echo "验证码Key: $CAPTCHA_KEY"
echo "验证码Code: $CAPTCHA_CODE"
echo ""

# 2. 登录获取Token
echo "2. 登录获取Token..."
LOGIN_RESPONSE=$(curl -s -X POST http://127.0.0.1:8000/api/user/auth/login \
  -H "Content-Type: application/json" \
  -d "{
    \"username\": \"admin\",
    \"password\": \"123456\",
    \"captcha\": \"$CAPTCHA_CODE\",
    \"captchaKey\": \"$CAPTCHA_KEY\"
  }")

echo "登录响应: $LOGIN_RESPONSE"

# 提取token
TOKEN=$(echo $LOGIN_RESPONSE | grep -o '"accessToken":"[^"]*"' | cut -d'"' -f4)
echo "获取到的Token: $TOKEN"
echo ""

if [ -z "$TOKEN" ]; then
    echo "❌ 登录失败，无法获取Token"
    exit 1
fi

# 3. 测试角色分页接口
echo "3. 测试角色分页接口..."
ROLE_PAGE_RESPONSE=$(curl -s -X GET "http://127.0.0.1:8000/api/user/roles/page?current=1&size=12" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "角色分页接口响应: $ROLE_PAGE_RESPONSE"
echo ""

# 检查响应状态
if echo "$ROLE_PAGE_RESPONSE" | grep -q '"code":200'; then
    echo "✅ 角色分页接口测试成功"
else
    echo "❌ 角色分页接口测试失败"
    echo "错误详情: $ROLE_PAGE_RESPONSE"
fi

# 4. 测试角色列表接口（不带page路径）
echo "4. 测试角色列表接口（不带page路径）..."
ROLE_LIST_RESPONSE=$(curl -s -X GET "http://127.0.0.1:8000/api/user/roles?current=1&size=12" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "角色列表接口响应: $ROLE_LIST_RESPONSE"
echo ""

# 检查响应状态
if echo "$ROLE_LIST_RESPONSE" | grep -q '"code":200'; then
    echo "✅ 角色列表接口测试成功"
else
    echo "❌ 角色列表接口测试失败"
    echo "错误详情: $ROLE_LIST_RESPONSE"
fi

# 5. 测试启用角色列表接口
echo "5. 测试启用角色列表接口..."
ENABLED_ROLES_RESPONSE=$(curl -s -X GET "http://127.0.0.1:8000/api/user/roles/enabled" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "启用角色列表接口响应: $ENABLED_ROLES_RESPONSE"
echo ""

# 检查响应状态
if echo "$ENABLED_ROLES_RESPONSE" | grep -q '"code":200'; then
    echo "✅ 启用角色列表接口测试成功"
else
    echo "❌ 启用角色列表接口测试失败"
    echo "错误详情: $ENABLED_ROLES_RESPONSE"
fi

# 6. 测试错误的路径（应该返回404）
echo "6. 测试错误的路径（应该返回404）..."
WRONG_PATH_RESPONSE=$(curl -s -X GET "http://127.0.0.1:8000/roles/page?current=1&size=12" \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json")

echo "错误路径响应: $WRONG_PATH_RESPONSE"
echo ""

# 检查是否返回404
if echo "$WRONG_PATH_RESPONSE" | grep -q '"status":404'; then
    echo "✅ 错误路径正确返回404"
else
    echo "❌ 错误路径没有返回404"
    echo "响应详情: $WRONG_PATH_RESPONSE"
fi

echo ""
echo "=== 测试完成 ==="
echo "结束时间: $(date)"
echo ""
echo "📊 测试总结:"
echo "- 角色分页接口: $(if echo "$ROLE_PAGE_RESPONSE" | grep -q '"code":200'; then echo "✅ 成功"; else echo "❌ 失败"; fi)"
echo "- 角色列表接口: $(if echo "$ROLE_LIST_RESPONSE" | grep -q '"code":200'; then echo "✅ 成功"; else echo "❌ 失败"; fi)"
echo "- 启用角色列表接口: $(if echo "$ENABLED_ROLES_RESPONSE" | grep -q '"code":200'; then echo "✅ 成功"; else echo "❌ 失败"; fi)"
echo "- 错误路径处理: $(if echo "$WRONG_PATH_RESPONSE" | grep -q '"status":404'; then echo "✅ 正确"; else echo "❌ 错误"; fi)" 